function getExtensionDir() {
    /*
     * Get's the extension directory
     * returns a nsIFile
     */

    var file = Components.classes["@mozilla.org/extensions/manager;1"]
        .getService(Components.interfaces.nsIExtensionManager)
        .getInstallLocation(BTGUID)
        .getItemLocation(BTGUID); // guid of extension 
    return file
}

function getAllInfo() {
    var req = new XMLHttpRequest();
    req.open('GET',"http://localhost:"+PORT+"/info_all",false);
    req.send(null);
//    dump('getAllInfo,'+req.responseText+"\n");
    if (req.responseText=="") return false;
    return req.responseText.parseJSON();
}

function getInfo(id) {
    var req = new XMLHttpRequest();
    req.open('GET',"http://localhost:"+PORT+"/info/?id="+id,false);
    req.send(null);
//    dump('getInfo,'+req.responseText+"\n");
    if (req.responseText=="") return false;
    return req.responseText.parseJSON();
}


function getPrefDir() {
    /*
     * The preferences folder of the profile in question, 
     * this way one could have several profiles 
     * and none of them will clobber the other
     *
     * returns a nsIFile
     */
    var file = Components.classes["@mozilla.org/file/directory_service;1"]
            .getService(Components.interfaces.nsIProperties)
            .get("PrefDOverride", Components.interfaces.nsIFile);
    return file
}
function getTorrentFilesDir() {
    /*
     *  Returns the nsIFile object that points to the 
     *  torrent meta file save location
     *  returns a nsIFile
     */
    var file=getPrefDir();
    file.append('bt');
    file.append('torrentfiles');
    return file
}

function getSaveDirFromUser() {
    /*
     * The destination folder needs to be queried from the user
     * returns a nsIFile
     */
    const nsIFilePicker = Components.interfaces.nsIFilePicker;
    var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker);
    fp.init(window, "Select Directory to Save Torrent in", nsIFilePicker.modeGetFolder);
    fp.appendFilters(nsIFilePicker.filterAll | nsIFilePicker.filterText);
    var rv = fp.show();
    /* BUILDING LINE */
//    var file = Components.classes["@mozilla.org/file/local;1"].
//                createInstance(Components.interfaces.nsILocalFile);
//    file.initWithPath('/home/general/Downloads');
//    return file
    /* /BUILDINGLINE */
    if (rv == nsIFilePicker.returnOK || rv == nsIFilePicker.returnReplace) {
        return fp.file;
    }
}
